package com.kycxika.app.activity.fragment;

import android.annotation.TargetApi;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.kycxika.app.R;
import com.kycxika.app.activity.MainActivity;
import com.lynnchurch.horizontalscrollmenu.BaseAdapter;
import com.lynnchurch.horizontalscrollmenu.HorizontalScrollMenu;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;



/**
 * Created by DELL on 2017/5/2.
 */
public class VideoFragment extends Fragment {
    private View mView;
    private HorizontalScrollMenu hsm_container;

    private final String[] data = {"Hello,Word!",
            "Hello,Android!", "Hello,Material design!",
            "Hello,Word!", "Hello,Android!", "Hello,Material design!",
            "Hello,Word!", "Hello,Android!", "Hello,Material design!",
            "Hello,Word!", "Hello,Android!", "Hello,Material design!",
            "Hello,Word!", "Hello,Android!", "Hello,Material design!"};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = View.inflate(getActivity(), R.layout.fragment_school_home, null);
        initView();
        return mView;
    }

    public void initView() {
        hsm_container = (HorizontalScrollMenu) mView.findViewById(R.id.hsm_container);
        hsm_container.setSwiped(true);
        hsm_container.setAdapter(new MenuAdapter());
    }

    class MenuAdapter extends BaseAdapter {
        String[] names = new String[]
                {"推荐视频","精品视频","动画动漫","搞笑视频","惊悚视频","国产视频" };
        private ListView mFree_list;
        private LinearLayout mTop_left;
        private LinearLayout mBottom_left;
        private LinearLayout mTop_right;
        private LinearLayout mBottom_right;
        public int [] imageURL = {R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4};
        public String[] titleStr = {"a1","a2","a3","a4"};
        public String[] contextStr = {"a1a1a1a1a1a1a11111111","a2a2a2a2a2a2a2a2a2a2a2aaaaaaa","a3a3a3a3a3a3a3a3","a44444444444444444444444444444444444444"};
        public String[] Sum = {"99","111","22","555"};
        @Override
        public List<String> getMenuItems() {
            return Arrays.asList(names);
        }

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public List<View> getContentViews() {
            List<View> views = new ArrayList<View>();
            for (String str : names)
            {
                if(str.equals("推荐视频")) {
                    View v = LayoutInflater.from(getActivity()).inflate(
                            R.layout.fragment_school, null);
                    mTop_left = (LinearLayout) v.findViewById(R.id.top_left);
                    mBottom_left = (LinearLayout) v.findViewById(R.id.bottom_left);
                    mTop_right = (LinearLayout) v.findViewById(R.id.top_right);
                    mBottom_right = (LinearLayout) v.findViewById(R.id.bottom_right);
                    int j = 0;
                    for (int i = 0;i < imageURL.length;i++) {
                        addImage(imageURL[i],j);
                        j++;
                    }
                    views.add(v);
                }else if(str.equals("精品视频")){
                    View v = LayoutInflater.from(getActivity()).inflate(
                            R.layout.fragment_video_jp, null);
                    Toolbar toolbar = (Toolbar) v.findViewById(R.id.toolbar);
                    //设置ToolBar作为ActionBar
                    MainActivity.instance.setSupportActionBar(toolbar);
                   // getActivity().setActionBar(toolbar); //.setSupportActionBar(toolbar);
                   // ActionBar actionBar = getActivity().getActionBar();
                    ActionBar actionBar = MainActivity.instance.getSupportActionBar();
                    if (actionBar != null) {
                        //设置ActionBar左上角按钮
                        actionBar.setHomeAsUpIndicator(R.drawable.ic_launcher);
                        actionBar.setDisplayHomeAsUpEnabled(true);
                    }
                    DrawerLayout drawerLayout = (DrawerLayout) v.findViewById(R.id.drawer_main);
                    FloatingActionButton floatBtn = (FloatingActionButton) v.findViewById(R.id.float_btn);
                    floatBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(getContext(), "Float Button", Toast.LENGTH_SHORT).show();
                        }
                    });

                    RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.recycler);
                    MyAdapter myAdapter = new MyAdapter();
                    recyclerView.setAdapter(myAdapter);
                    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                    views.add(v);
                }else {
                    View v = LayoutInflater.from(getActivity()).inflate(
                            R.layout.content_view, null);
                    TextView tv = (TextView) v.findViewById(R.id.tv_content);
                    tv.setText(str);
                    views.add(v);
                }
            }
            return views;
        }

        private void addImage(int i, int j) {
            ImageView image = new ImageView(getActivity());
            TextView title = new TextView(getActivity());
            TextView contextTxt = new TextView(getActivity());
            RelativeLayout relati = new RelativeLayout(getActivity());
            TextView author = new TextView(getActivity());
            TextView oksum = new TextView(getActivity());
            LinearLayout.LayoutParams imageparams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams titleparams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            imageparams.weight = 12;
            titleparams.weight = 1;
            titleparams.gravity = Gravity.CENTER|Gravity.CENTER_VERTICAL;
            image.setLayoutParams(imageparams);
            title.setLayoutParams(titleparams);
            contextTxt.setLayoutParams(titleparams);
            image.setImageResource(i);
            image.setPadding(10,10,10,10);
            title.setText(titleStr[j]);
            title.setTextColor(Color.BLACK);
            title.setTextSize(22);
            title.setPadding(10,10,10,10);
            contextTxt.setText(contextStr[j]);
            contextTxt.setTextSize(20);
            contextTxt.setTextColor(Color.GRAY);
            contextTxt.setMaxLines(2);
            contextTxt.setPadding(8,0,5,0);
            author.setText(titleStr[j]);
            oksum.setText(Sum[j]);

            RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            lp.setMargins(8,0,0,5);
            author.setLayoutParams(lp);   ////设置按钮的布局属性
            lp=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            lp.setMargins(0,0,8,0);
            oksum.setLayoutParams(lp);   ////设置按钮的布局属性
           // setContentView(relative);
            relati.addView(author);
            relati.addView(oksum);
            if(j == 0){
                mTop_left.addView(image);
                mTop_left.addView(title);
                mTop_left.addView(contextTxt);
                mTop_left.addView(relati);
            }else if(j == 1) {
                mTop_right.addView(image);
                mTop_right.addView(title);
                mTop_right.addView(contextTxt);
                mTop_right.addView(relati);
            }else if(j == 2){
                mBottom_left.addView(image);
                mBottom_left.addView(title);
                mBottom_left.addView(contextTxt);
                mBottom_left.addView(relati);
            }else {
                mBottom_right.addView(image);
                mBottom_right.addView(title);
                mBottom_right.addView(contextTxt);
                mBottom_right.addView(relati);
            }
        }

        @Override
        public void onPageChanged(int position, boolean visitStatus) {
          /*  Toast.makeText(getActivity(),
                    "内容页：" + (position + 1) + " 访问状态：" + visitStatus,
                    Toast.LENGTH_SHORT).show();*/
        }
    }

     class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

        class ViewHolder extends RecyclerView.ViewHolder {
            private TextView tv;

            ViewHolder(View itemView) {
                super(itemView);
                tv = (TextView) itemView.findViewById(R.id.tv);
            }

        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.jp_item_list, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.tv.setText(data[holder.getAdapterPosition()]);
        }

        @Override
        public int getItemCount() {
            return data.length;
        }
    }
}
